# -*- coding:utf-8 -*-

from fastapi import APIRouter, Depends, Form
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request
from typing import List
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.entity.vo.import_vo import ImportModel
from module_admin.service.import_service import ImportService
from module_admin.service.login_service import LoginService
from module_admin.aspect.data_scope import GetDataScope
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.annotation.log_annotation import Log
from utils.response_util import ResponseUtil
from utils.common_util import bytes2file_response

from module_product.entity.vo.bg_product_vo import BgProductPageModel, BgProductModel
from module_product.service.bg_product_service import BgProductService

bgProductController = APIRouter(prefix='/bg/product')


@bgProductController.get('/list')
async def get_bg_product_list(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        page_query: BgProductPageModel = Depends( BgProductPageModel.as_query),
):
    bg_product_result = await BgProductService.get_bg_product_list(query_db, page_query, None)

    return ResponseUtil.success(model_content=bg_product_result)

@bgProductController.get('/getById/{bgProductId}', dependencies=[Depends(CheckUserInterfaceAuth('bg:product:query'))])
async def get_bg_product_by_id(
        request: Request,
        bgProductId: int,
        query_db: AsyncSession = Depends(get_db),
        data_scope_sql: str = Depends(GetDataScope('BgProduct'))
):
    bg_product = await BgProductService.get_bg_product_by_id(query_db, bgProductId)
    return ResponseUtil.success(data=bg_product)

@bgProductController.get('/popular', dependencies=[Depends(CheckUserInterfaceAuth('bg:product:query'))])
async def get_popular_products(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        limit: int = 3
):
    """
    获取热门套餐
    """
    popular_products = await BgProductService.get_popular_products(query_db, limit)
    return ResponseUtil.success(data=popular_products)


@bgProductController.post('/add', dependencies=[Depends(CheckUserInterfaceAuth('bg:product:add'))])
@Log(title='bg_product', business_type=BusinessType.INSERT)
async def add_bg_product (
    request: Request,
    add_model: BgProductModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):

    add_model.create_by = current_user.user.user_id
    add_model.dept_id = current_user.user.dept_id
    add_dict_type_result = await BgProductService.add_bg_product(query_db, add_model)
    return ResponseUtil.success(data=add_dict_type_result)

@bgProductController.put('/update', dependencies=[Depends(CheckUserInterfaceAuth('bg:product:edit'))])
@Log(title='bg_product', business_type=BusinessType.UPDATE)
async def update_bg_product(
    request: Request,
    edit_model: BgProductModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_dict_type_result = await BgProductService.update_bg_product(query_db, edit_model)
    return ResponseUtil.success(data=add_dict_type_result)


@bgProductController.delete('/delete/{bgProductIds}', dependencies=[Depends(CheckUserInterfaceAuth('bg:product:remove'))])
@Log(title='bg_product', business_type=BusinessType.DELETE)
async def del_bg_product(
    request: Request,
    bgProductIds: str,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    ids = bgProductIds.split(',')
    del_result = await BgProductService.del_bg_product(query_db, ids)
    return ResponseUtil.success(data=del_result)

@bgProductController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('bg:product:export'))])
@Log(title='bg_product', business_type=BusinessType.EXPORT)
async def export_bg_product(
    request: Request,
    bg_product_form: BgProductPageModel = Form(),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('BgProduct')),
):
    # 获取全量数据
    export_result = await BgProductService.export_bg_product_list(
        query_db, bg_product_form, data_scope_sql
    )
    return ResponseUtil.streaming(data=bytes2file_response(export_result))

@bgProductController.post('/import', dependencies=[Depends(CheckUserInterfaceAuth('bg:product:import'))])
async def import_bg_product(request: Request,
                      import_model: ImportModel,
                      query_db: AsyncSession = Depends(get_db),
                      current_user: CurrentUserModel = Depends(LoginService.get_current_user)
    ):
    """
    导入数据
    """
    await ImportService.import_data(query_db, import_model, current_user)
    return ResponseUtil.success()